package com.example.wx.controller;

import com.example.wx.config.WxConfig;
import com.example.wx.utils.JsonUtils;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class WXPayController
{
    public static final String NOTIFY_URL = "http://www.wrhyd.online";
    public static final String TRADE_TYPE_JSAPI = "JSAPI";

    @RequestMapping("/doUnifiedOrder")
    @ResponseBody
    public JsonUtils doUnifiedOrder(String openId)
    {
        Map resultMap = new HashMap();
        WxConfig config = null;
        WXPay wxpay = null;
        try
        {
            config = new WxConfig();
            wxpay = new WXPay(config);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        //生成的随机字符串
        String nonce_str = WXPayUtil.generateNonceStr();
        //获取客户端的ip地址
        //获取本机的ip地址
        InetAddress addr = null;
        try
        {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
        String spbill_create_ip = addr.getHostAddress();
        //支付金额，需要转成字符串类型，否则后面的签名会失败
        int total_fee = 1;
        //商品描述
        String body = "商品支付";
        //商户订单号
        String out_trade_no = WXPayUtil.generateNonceStr();

        //统一下单接口参数
        HashMap<String, String> data = new HashMap<String, String>();
        try
        {
            data.put("appid", config.getAppID());
            data.put("body", body);
            data.put("mch_id", config.getMchID());
            data.put("nonce_str", nonce_str);
            data.put("notify_url", NOTIFY_URL);
            data.put("openid", openId);
            data.put("out_trade_no", out_trade_no);
            data.put("spbill_create_ip", spbill_create_ip);
            data.put("total_fee", String.valueOf(total_fee));
            data.put("trade_type", TRADE_TYPE_JSAPI);
            data.put("sign", WXPayUtil.generateSignature(data, config.getKey(), WXPayConstants.SignType.MD5));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            Map<String, String> rMap = wxpay.unifiedOrder(data);
            System.out.println("统一下单接口返回: " + rMap);
            String return_code = (String) rMap.get("return_code");
            String result_code = (String) rMap.get("result_code");
            String nonceStr = WXPayUtil.generateNonceStr();
            resultMap.put("nonceStr", nonceStr);
            Long timeStamp = System.currentTimeMillis() / 1000;
            if ("SUCCESS".equals(return_code) && return_code.equals(result_code))
            {
                String prepayid = rMap.get("prepay_id");
                resultMap.put("package", "prepay_id=" + prepayid);
                resultMap.put("signType", "MD5");
                //这边要将返回的时间戳转化成字符串，不然小程序端调用wx.requestPayment方法会报签名错误
                resultMap.put("timeStamp", timeStamp + "");
                //再次签名，这个签名用于小程序端调用wx.requesetPayment方法
                resultMap.put("appId", config.getAppID());
                String sign = WXPayUtil.generateSignature(resultMap, config.getKey());
                resultMap.put("paySign", sign);
                System.out.println("生成的签名paySign : " + sign);
                //response.setData(resultMap);
                return JsonUtils.success("成功！", resultMap);
            } else
            {
                return JsonUtils.error("支付失败");
            }
        } catch (Exception e)
        {
            e.printStackTrace();
            return JsonUtils.error("统一下单接口返回失败");
        }
    }
}
